package cn.wit.test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 167.两数之和
 * @author 16604
 *
 */
public class DemoAddTwoII {
	public static void main(String[] args) {
		
		int[] nums= {2,7,11,15};
		int target = 9;
		System.out.println(Arrays.toString(twoSum(nums,target)));
		
	}
	
	public static int[] twoSum(int[] numbers,int target) {
		
		Map<Integer,Integer> map =new HashMap<>();
		
		//尝试，转换成map
		for(int i =0;i<numbers.length;i++) {
			map.put(numbers[i], i);
		}
		
		for(int i =0;i<numbers.length;i++) {
			int j = target - numbers[i];

			if(map.containsKey(j)) {
//				System.out.println(j+" "+numbers[i]);
//				System.out.println(i+" "+map.get(j));
				
				return new int[] {(i+1),(map.get(j)+1)};
			
			}
		}
		throw new RuntimeException("无解");
	}
}
